File Handling for All

Appendix F:
The Associated Programs Disc

This appendix provides information about the contents of the programs disc associated with this book. The programs fall into three categories: those listed as complete programs in the book, those based upon routines (functions and procedures) from the book, and additional programs providing further examples of file handling, or utilities of use to those developing their own programs.

The programs are described in the order in which they appear (or are referred to) in the book by chapter. All the programs on the disc except one are named in a uniform way: the name consists of the word 'Files' followed by a two-digit number indicating the chapter to which the program relates, and its position in sequence within the programs in that chapter. For example, a program called Files23 would be the third program in chapter 2. The purpose/function of each program is described below. In each case details are given of any sample data files also included on the disc, which may be used as demonstrations of the way the programs work. You are advised to make a copy of the programs and files on this disc before you start experimenting to avoid accidentally deleting or corrupting any of its contents.

RUDIMENTARY FILE HANDLING (CHAPTER 1)

There are two programs in this section, Files11 (Create) and Files12 (Update), together with a data file League. The Create program can be used to initialise a football league table, by entering the number and names of the teams to take part. The Update program will prompt for the details of a set of matches and update the league table accordingly. The data file League supplied already contains some data, and you should therefore be careful, if you use the Create program, as this will overwrite and thus re-initialise this file unless it is renamed or copied to another disc first.

SIMPLE SERIAL FILES (CHAPTER 2)

There are four programs described in this chapter with functions as follows:

Files21 Create file Creates a DATES file and prompts for initial data.
Files22 Display records Display contents of DATES file on screen.
Files23 Update file Add new records to an existing DATES file.
Files24 Amend file Modify or delete existing records in DATES file.

All the programs implicitly use a data file called DATES, and a sample file with this name is included on the disc. Do not use the program Files21 unless you have renamed the original DATES file or saved a copy to another disc. Note that when using program Files24, a new copy of the DATES file is created, with the old copy being renamed as OLDATES.

The purpose of this suite of programs is to provide a simple database for storing and retrieving the details of the names, addresses, phone numbers and birthdays of one's friends, relatives and acquaintances. All the programs prompt clearly for the information required. In each case, the relevant data, address for example, should be entered as a single string of characters, pressing Return once only to conclude input of the data for that field. No specific format is otherwise required, and birthdays (for example) can be in any style you choose. It does usually pay to be consistent though.

A PROFESSIONAL APPROACH TO FILE HANDLING (CHAPTER 3)

There is one quite substantial program associated with this chapter named Files31. This program incorporates all the procedures and functions described in this chapter, together with additional code and routines to form a complete working program. The program is menu driven for simplicity with the following eight options available:

Create File Create a new empty file using information from the user.
Open file Open a previously created file for use.
Add Record Add a new record to the file.
Display Record Display one or more records from a file.
Delete Record Delete a record from the file.
Update Record Update/amend an existing record in the file.
Close File Close the currently open file.
Exit Terminate execution of the program, closing all open files.

When using the Create option you will need to specify the name of the file to be created, the (maximum) number of records it is intended to store, the number of data fields, and for each field its name (maximum 10 characters), field width (maximum 99), and a field type (a single digit). The program currently treats all data as of type string (type 0), but functions/procedures exist within the program which you could expand to provide additional field types. Once all the details have been entered you are asked to confirm these before the new file is created.

A sample data file called NAMES1 is supplied on the disc for use with this program. The fields are Name, Address1, Address2, PCode, Comment. You can add further records to this file or modify those already there, but note the original warning about saving a secure copy before experimenting. You can, of course, create any further files you wish of your own design.

LINKED RECORDS AND FILES (CHAPTER 5)

Two programs are included on the disc illustrating the points dealt with in chapter 5, and incorporating the various procedures and functions listed there. Both programs use an identical menu-based approach as described for the previous program.

The first program from chapter 5 is called Files51 and this demonstrates the use of a single internal pointer system to chain records together. The program functions similarly to Files31 except that when a new file is being created you are asked to specify one of the data fields as a key field. Once that has been done all records in the file are maintained as a linked chain in the order determined by the contents of the key field for each record.

When a file is first created, all its records are chained together to form a free list. When a record is entered in the file, the program obtains the first empty record from the free list, inserts the data entered by the user, and adds this record to the chain of live records. When a record is deleted, it is removed from the live chain and returned to the free chain. The File Description Record (FDR) maintains a reference to which field is the key field, and pointers to the records which are currently the heads of the live and free chains respectively. Because records are ordered according to the content of their key fields, the program does not allow the contents of this field to be updated, when otherwise modifying the contents of a record.

A sample data file called NAMES2 is supplied on the disc for use with Files51. This follows the same format as the previous data file NAMES1 (with the same fields), except that the Name field has been selected as the key field, and all the records in the file will be linked in order of name.

A second program, Files52, takes the simpler approach of Files51 much further. It implements both primary and secondary internal pointers, and allows the use of pointer fields to link together the records in two related files. The use of the program is perhaps best explained by reference to the two sample data files supplied on disc, ORDERS and ITEMS. The ORDERS file contains details of items ordered by a number of customers. Each order record consists of four fields, NAME, DATE, CODE and QTY (quantity) representing an order from one customer for one item. Where a customer sends in an order for several items, this will result in a series of linked records using secondary pointers. An order from a second customer will produce a further linked list, again using secondary pointers, while the heads of these two lists will be linked (in order by customer name) using the primary pointer.

The second data file, ITEMS, created with the same program, consists of a simply linked chain of records (using the primary pointer) which contains two fields: a code number and a description for each item which a customer might order. These records can be displayed and updated as required. When the ORDERS file is updated, each item is specified by a code. This is used as a key to search the related file, ITEMS (where code must be the key field), to obtain the corresponding description, which is displayed in place of the code, and a pointer value to the record containing these details. This pointer value is then stored in the code field, and is used whenever the records from the ORDERS file are displayed to show the correct description for each item.

Note that when creating a file, the field type must be specified as type 5 for it to be treated as a pointer field (in which case you will be prompted for the name of the related file). Otherwise, as with the previous programs, all fields are treated as of type string, although the embryo functions exist in the program for other field types.

PRACTICAL ADVICE (CHAPTER 9)

Chapter 9, on practical program development, contains the listing for one program, Files91, and there is a reference to a second, Files92 (a data file super-dump). Both of these programs are in the form of utilities designed to assist the process of program development when dealing with file handling. The program Files91 allows individual bytes within a data file to be changed. You simply specify the name of the data file to be operated upon, and then the address and new value required at that address. Confirmation is sought before the change in each case. The process is repeated until Escape is pressed. This can be handy for patching up test data files which have become corrupted by bugs within the program under development.

The second utility, Files92, can prove a useful adjunct to the *DUMP command by attempting to interpret the integer, real and string data types produced when writing data to a file using PRINT#. The utility will, as far as possible, produce a list of identified items, giving the address (PTR# value), data type, and value of item identified. Do be aware though, that a corrupted data file may be misinterpreted by the program, producing spurious results.

FILER DATABASE MANAGEMENT SYSTEM

This forms a comprehensive database system in its own right, and illustrates many of the principles described in this book, particularly those of chapter three. As it is not referred to in the main part of the book a full description is given separately in Appendix B, to which the reader should refer.

OBTAINING THE ASSOCIATED DISC

The disc of programs described in this appendix may be obtained direct from the publisher for £4.75 inc VAT plus 60p post & packing. The disc is available in 3.5" format for the Archimedes, and in 3.5" ADFS format or 5.25" 40/80 track DFS format for the BBC micro and Master series. Please make sure that you specify the correct format of disc when ordering.